package com.dongdongshop.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.Enum.ResultEnum;
import com.dongdongshop.core.SpecificPrint;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.TbBrandServiceI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("tbBrandController")
public class TbBrandController {

    @Reference //引用dubbo服务接口，不能使用  @Autowired
    private TbBrandServiceI tbBrandServiceI;

    private Logger logger= LoggerFactory.getLogger(TbBrandController.class);

    //查询商品管理数据
    @RequestMapping("getFindAll")
    public String getFindAll(Model model){
         try {
             List<TbBrand> tbBrands = tbBrandServiceI.getFindAll();
             model.addAttribute("tbl",tbBrands);
             logger.info(ResultEnum.SUCCESS.getMessage()+"，查询结果为：{}",tbBrands);
         }catch (Exception e){
             logger.error(ResultEnum.ERROR.getMessage()+":{}",e);
         }
        return "admin/brand";
    }

    //查询回显
    @RequestMapping("toUpdate")
    @ResponseBody
    public TbBrand toUpdate(Integer id){
         try {
             TbBrand tbBrand = tbBrandServiceI.selectTbBrandById(id);
             logger.info(ResultEnum.SUCCESS.getMessage()+"，查询结果为：{}",tbBrand);
             return tbBrand;
         }catch (Exception e){
             logger.error(ResultEnum.ERROR.getMessage()+":{}",e);
             return null;
         }

    }

    //确认修改
    @RequestMapping("updateTbBrand")
    @ResponseBody
    public SpecificPrint updateTbBrand(TbBrand tbBrand){
         try {
             tbBrandServiceI.updateTbBrand(tbBrand);
             logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",tbBrand);
             return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbBrand);
         }catch (Exception e){
             logger.error(ResultEnum.ERROR.getMessage(),e);
             return SpecificPrint.response(ResultEnum.ERROR);
         }

    }

    //增加
    @RequestMapping("addTbBrand")
    @ResponseBody
    public SpecificPrint addTbBrand(TbBrand tbBrand){
         try{
             tbBrandServiceI.addTbBrand(tbBrand);
             logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",tbBrand);
             return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbBrand);
         }catch (Exception e){
             logger.error(ResultEnum.ERROR.getMessage(),e);
             return SpecificPrint.response(ResultEnum.ERROR);
         }
    }

    //删除
    @RequestMapping("deleteTbBrand")
    @ResponseBody
    public SpecificPrint deleteTbBrand(String ids){
        //截取字符串
        String idss=ids.replaceAll("'","");
        String[] split = idss.split(",");
        //遍历出所有选中的ID
        List<Integer> list=new ArrayList<>();
        for (String s : split) {
            list.add(Integer.valueOf(s));
        }
        try {
            tbBrandServiceI.deleteTbBrand(list);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",list);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }
}
